package user

import (
	"context"
	"strconv"
	"test-demo/common"
	"test-demo/user_srv/user"
	"time"

	"test-demo/api/internal/svc"
	"test-demo/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginLogic) Login(req *types.LoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	res, _ := l.svcCtx.UserServer.Login(context.Background(), &user.LoginRequest{
		Username: req.Username,
		Password: req.Password,
	})
	if res.Status == "0" {
		return &types.Response{
			Code:    10001,
			Message: "登陆失败",
			Data:    nil,
		}, nil
	}
	ref, _ := strconv.Atoi(res.Status)
	token, _ := common.GetToken(l.svcCtx.Config.AuthToken.AccessSecret, l.svcCtx.Config.AuthToken.AccessExpire, time.Now().Unix(), ref)
	return &types.Response{
		Code:    200,
		Message: "登陆成功",
		Data: map[string]string{
			"token": token,
		},
	}, nil
}
